package com.ss.android.common.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.ss.android.article.browser.R;

import java.lang.ref.WeakReference;

public final class AlertController {
    public static final class AlertParams {
        public final Context mContext;
        public final LayoutInflater mInflater;
        public CharSequence mTitle;
        public CharSequence mMessage;
        public CharSequence mPositiveButtonText;
        public DialogInterface.OnClickListener mPositiveButtonListener;
        public CharSequence mNegativeButtonText;
        public DialogInterface.OnClickListener mNegativeButtonListener;
        public CharSequence mNeutralButtonText;
        public DialogInterface.OnClickListener mNeutralButtonListener;
        public boolean mCancelable;
        public DialogInterface.OnCancelListener mOnCancelListener;
        public CharSequence[] mItems;
        public DialogInterface.OnClickListener mOnItemSelectedListener;
        public View mCustomTitleView;
        public boolean mIsSingleChoice;
        public int mCheckedItem = -1;

        public AlertParams(Context arg2) {
            this.mContext = arg2;
            this.mCancelable = true;
            this.mInflater = (LayoutInflater) arg2.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
    }

    final class ButtonHandler extends Handler {
        private WeakReference<DialogInterface> mDialog;

        public ButtonHandler(DialogInterface arg2) {
            this.mDialog = new WeakReference(arg2);
        }

        public final void handleMessage(Message arg4) {
            switch (arg4.what) {
                case -3:
                case -2:
                case -1: {
                    ((DialogInterface.OnClickListener) arg4.obj).onClick(this.mDialog.get(), arg4.what);
                    break;
                }
                case 1: {
                    ((DialogInterface) arg4.obj).dismiss();
                    break;
                }
            }
        }
    }

    static final class CheckedItemAdapter extends ArrayAdapter<CharSequence> {
        public CheckedItemAdapter(Context arg2, int arg3, CharSequence[] arg4) {
            super(arg2, arg3, R.id.text1, arg4);
        }

        public final long getItemId(int arg3) {
            return ((long) arg3);
        }

        public final boolean hasStableIds() {
            return true;
        }
    }

    Handler mHandler;
    final View.OnClickListener mButtonHandler = new com.ss.android.common.dialog.b(this);
    final Context mContext;
    final DialogInterface mDialogInterface;
    final Window mWindow;
    CharSequence mTitle;
    CharSequence mMessage;
    ListView mListView;
    View mCustomTitleView;
    public Button mButtonPositive;
    CharSequence mButtonPositiveText;
    Message mButtonPositiveMessage;
    public Button mButtonNegative;
    CharSequence mButtonNegativeText;
    Message mButtonNegativeMessage;
    public Button mButtonNeutral;
    CharSequence mButtonNeutralText;
    Message mButtonNeutralMessage;
    ScrollView mScrollView;
    ImageView mIconView;
    TextView mTitleView;
    TextView mMessageView;
    ListAdapter mAdapter;
    int mCheckedItem = -1;
    int mAlertDialogLayout;
    int mListLayout;
    int mMultiChoiceItemLayout;
    int mSingleChoiceItemLayout;

    public AlertController(Context arg2, DialogInterface arg3, Window arg4) {
        this.mContext = arg2;
        this.mDialogInterface = arg3;
        this.mWindow = arg4;
        this.mHandler = new ButtonHandler(arg3);
        this.mAlertDialogLayout = R.layout.ss_alert_dialog;
        this.mListLayout = R.layout.ss_select_dialog;
        this.mMultiChoiceItemLayout = R.layout.ss_select_dialog_singlechoice;
        this.mSingleChoiceItemLayout = R.layout.ss_select_dialog_item;
    }

    static void manageScrollIndicators(View arg3, View arg4, View arg5) {
        int v2 = View.INVISIBLE;
        int v1 = 0;
        if (arg4 != null) {
            int v0 = arg3.canScrollVertically(-1) ? View.VISIBLE : v2;
            arg4.setVisibility(v0);
        }

        if (arg5 != null) {
            if (!arg3.canScrollVertically(1)) {
                v1 = v2;
            }

            arg5.setVisibility(v1);
        }
    }

    static boolean canTextInput(View arg4) {
        boolean v0 = true;
        if (!arg4.onCheckIsTextEditor()) {
            if (!(arg4 instanceof ViewGroup)) {
                v0 = false;
            } else {
                int v2 = ((ViewGroup) arg4).getChildCount();
                do {
                    if (v2 > 0) {
                        --v2;
                        if (!canTextInput(((ViewGroup) arg4).getChildAt(v2))) {
                            continue;
                        }

                        return v0;
                    } else {
                        return false;
                    }
                }
                while (true);
            }
        }

        return v0;
    }

    public final void setButton(int arg3, CharSequence arg4, DialogInterface.OnClickListener arg5, Message arg6) {
        if (arg5 != null) {
            arg6 = this.mHandler.obtainMessage(arg3, arg5);
        }

        switch (arg3) {
            case -3:
                this.mButtonNeutralText = arg4;
                this.mButtonNeutralMessage = arg6;
                return;
            case -2:
                this.mButtonNegativeText = arg4;
                this.mButtonNegativeMessage = arg6;
                return;
            case -1:
                this.mButtonPositiveText = arg4;
                this.mButtonPositiveMessage = arg6;
                return;
        }
        throw new IllegalArgumentException("Button does not exist");


    }

    public final void setTitle(CharSequence arg2) {
        this.mTitle = arg2;
        if (this.mTitleView != null) {
            this.mTitleView.setText(arg2);
        }
    }
}

